package com.txw.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txw.commonutils.R;
import com.txw.serviceedu.entity.EduCourse;
import com.txw.serviceedu.entity.vo.CourseInfoVo;
import com.txw.serviceedu.entity.vo.CoursePublishVo;
import com.txw.serviceedu.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author TanXiongWei
 * @since 2021-11-28
 */
@RestController
@RequestMapping("/serviceedu/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

//    //课程列表查询
//    @GetMapping
//    @ApiOperation(value = "课程列表查询")
//    public R getCourseList() {
//        List<EduCourse> list = eduCourseService.list(null);
//        return R.ok().data("list", list);
//    }

    //多条件组合课程列表查询
    @PostMapping("getPublishCourseList/{current}/{limit}")
    public R getPublishCourseList(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) EduCourse course) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(course.getTitle())) {
            queryWrapper.like("title", course.getTitle());
        }
        if (!StringUtils.isEmpty(course.getStatus())) {
            queryWrapper.eq("status", course.getStatus());
        }
        eduCourseService.page(coursePage, queryWrapper);
        return R.ok().data("total", coursePage.getTotal()).data("rows", coursePage.getRecords());
    }


    //添加课程基本信息方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据课程查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo", courseInfoVo);
    }


    //修改课程基本信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //删除课程信息
    @DeleteMapping("{courseId}")
    @ApiOperation(value = "删除课程信息")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

    /**
     * 根据课程id 查询课程确认信息
     *
     * @param id
     * @return
     */
    @GetMapping("getPublishCourseInfo/{id}")
    @ApiOperation(value = "根据课程id 查询课程确认信息")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    //课程最终发布
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

}

